package com.ygqh.baby.service.impl;

import com.wdt.api.response.support.StockOutOrderTradeDTO;
import com.wdt.api.response.support.StockOutOrderTradeDetail;
import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.WdtQybStockOutDetailMapper;
import com.ygqh.baby.mapper.WdtQybStockOutMapper;
import com.ygqh.baby.po.WdtQybStockOut;
import com.ygqh.baby.po.WdtQybStockOutDetail;
import com.ygqh.baby.po.WdtQybStockOutExample;
import com.ygqh.baby.service.WdtQybStockOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class WdtQybStockOutServiceImpl implements WdtQybStockOutService {

    @Autowired
    private WdtQybStockOutMapper wdtQybStockOutMapper;
    @Autowired
    private WdtQybStockOutDetailMapper wdtQybStockOutDetailMapper;

    @Override
    public List<WdtQybStockOut> find() {
        WdtQybStockOutExample example = new WdtQybStockOutExample();
        example.createCriteria();
        return wdtQybStockOutMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        WdtQybStockOutExample example = new WdtQybStockOutExample();
        example.createCriteria();
        return wdtQybStockOutMapper.countByExample(example);
    }

    @Override
    public WdtQybStockOut findById(Long id) {
        return wdtQybStockOutMapper.selectByPrimaryKey(id);
    }

    @Override
    public int save(WdtQybStockOut wdtQybStockOut) {
        return wdtQybStockOutMapper.insertSelective(wdtQybStockOut);
    }

    @Override
    public int update(WdtQybStockOut wdtQybStockOut) {
        return wdtQybStockOutMapper.updateByPrimaryKeySelective(wdtQybStockOut);
    }

    @Override
    public ResultSet<WdtQybStockOut> search(QueryInfo queryInfo, String q) {
        List<WdtQybStockOut> list = wdtQybStockOutMapper.selectSuper(queryInfo, q);
        int count = wdtQybStockOutMapper.countSuper(q);
        ResultSet<WdtQybStockOut> resultSet = new ResultSet<WdtQybStockOut>(count, list);
        return resultSet;
    }

    @Override
    public int saveOrUpdate(WdtQybStockOut wdtQybStockOut) {
        int r = 0;
        if (wdtQybStockOut.getStockoutId() != null) {
            r = this.update(wdtQybStockOut);
        } else {
            r = this.save(wdtQybStockOut);
        }
        return r;
    }

    @Override
    public int addBatch(List<WdtQybStockOut> outList) {
        int row = 0;
        if (!CollectionUtils.isEmpty(outList)) {
            row = wdtQybStockOutMapper.insertBatch(outList);
            List<WdtQybStockOutDetail> details = new ArrayList<WdtQybStockOutDetail>();
            for (int i = 0; i < outList.size(); i++) {
                details.addAll(outList.get(i).getDetailsList());
            }
            if (!CollectionUtils.isEmpty(details)) {
                wdtQybStockOutDetailMapper.insertBatch(details);
            }
        }
        return row;
    }

    @Override
    public int addBatchByDTO(List<StockOutOrderTradeDTO> outList) {
        int row = 0;
        if (!CollectionUtils.isEmpty(outList)) {
            row = wdtQybStockOutMapper.insertBatchByDTO(outList);
            List<StockOutOrderTradeDetail> details = new ArrayList<>();
            for (int i = 0; i < outList.size(); i++) {
                details.addAll(outList.get(i).getDetailList());
            }
            if (!CollectionUtils.isEmpty(details)) {
                wdtQybStockOutDetailMapper.insertBatchV2(details);
            }
        }
        return row;
    }

    @Override
    public int deleteBatch(List<Integer> stockOutIds) {
        if (CollectionUtils.isEmpty(stockOutIds)) {
            return 0;
        }
        int i = wdtQybStockOutMapper.deleteBatch(stockOutIds);
        if (i > 0) {
            wdtQybStockOutDetailMapper.deleteBatch(stockOutIds);
        }
        return i;
    }

    @Override
    public WdtQybStockOut findStockOutByLogisticsNo(Long logisticsNo) {
        return wdtQybStockOutMapper.selectStockOut(logisticsNo);

    }

}
